
<template>
  <div>
    <el-popover ref="elPopover" placement="bottom-start" popper-class="tree-popover" trigger="click" clearable @hide="popoverHide">
      <el-tree ref="tree" class="common-tree tree-line" clearable :data="data" :props="defaultProps" :show-checkbox="multiple" :node-key="nodeKey" :check-strictly="checkStrictly" :default-expand-all="defaultExpandAll" :expand-on-click-node="true" :default-checked-keys="defaultCheckedKeys" :highlight-current="true" @node-click="handleNodeClick" @check-change="handleCheckChange" />
      <el-select slot="reference" ref="select" v-model="selectedData" :multiple="multiple" collapse-tags clearable class="tree-select" @click.native="isShowSelect = !isShowSelect">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
      </el-select>
    </el-popover>
  </div>
</template>

<script>
export default {
  name: 'TreeSelect',
  // props: ["clear"],
  props: {
    el: {
      type: String,
      default() {
        return ''
      }
    },
    // 树结构数据
    data: {
      type: Array,
      default() {
        return []
      }
    },
    // 是否可选根节点
    rootNodeChick: Boolean,
    // 是否清空数据
    clear: {
      type: Number,
      default: 0
    },
    defaultProps: {
      type: Object,
      default: () => ({
        parent: 'parentId', // 父级唯一标识
        value: 'id', // 唯一标识
        label: 'label', // 标签显示
        children: 'children' // 子级
      })
    },
    // 是否展开全部子节点
    defaultExpandAll: {
      type: Boolean,
      default: true
    },
    // 配置是否可多选
    multiple: {
      type: Boolean,
      default() {
        return false
      }
    },
    nodeKey: {
      type: String,
      default() {
        return 'id'
      }
    },
    // 显示复选框情况下，是否严格遵循父子不互相关联
    checkStrictly: {
      type: Boolean,
      default() {
        return false
      }
    },
    // 默认选中的节点key数组
    checkedKeys: {
      type: Array,
      default() {
        return []
      }
    },
    width: {
      type: Number,
      default() {
        return 260
      }
    },
    height: {
      type: Number,
      default() {
        return 360
      }
    }
  },
  data() {
    return {
      defaultCheckedKeys: [],
      isShowSelect: false, // 是否显示树状选择器
      options: [],
      selectedData: [], // 选中的节点
      // style: "width:" + this.width + "px;" + "height:" + this.height + "px;",
      // selectStyle: "width:" + (this.width + 24) + "px;",
      checkedIds: [],
      checkedData: []
    }
  },
  watch: {
    isShowSelect(val) {
      // 隐藏select自带的下拉框
      this.$refs.select.blur()
      // 自适应select宽度
      const selectWidth = this.$refs.select.$el.offsetWidth
      const popper = this.$refs.elPopover.$refs.popper
      popper.style.minWidth = selectWidth + 'px'
    },
    clear: function (n, o) {
      // 箭头函数  不然会发生this改变
      if (n != 0) {
        this.selectedData = []
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys([])
        })
      }
    },
    selectedData(newData, oldData) {
      this.popoverHide()
      if (newData == undefined || newData == null || newData == [] || newData.length == 0) {
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys([])
        })
      }
    }
  },
  mounted() {
    if (this.checkedKeys.length > 0) {
      if (this.multiple) {
        this.defaultCheckedKeys = this.checkedKeys
        this.selectedData = this.checkedKeys.map(item => {
          var node = this.$refs.tree.getNode(item)
          return node.label
        })
      } else {
        var item = this.checkedKeys[0]
        this.$refs.tree.setCurrentKey(item)
        var node = this.$refs.tree.getNode(item)
        this.selectedData = node.label
      }
    }
  },
  methods: {
    loadCheckedKeys() {
      if (this.checkedKeys.length > 0) {
        if (this.multiple) {
          this.defaultCheckedKeys = this.checkedKeys
          this.selectedData = this.checkedKeys.map(item => {
            var node = this.$refs.tree.getNode(item)
            return node.label
          })
        } else {
          var item = this.checkedKeys[0]
          this.$refs.tree.setCurrentKey(item)
          var node = this.$refs.tree.getNode(item)
          this.selectedData = node.label
        }
      }
    },
    changeIsShowSelect() {
      this.isShowSelect = false
    },
    popoverHide() {
      if (this.multiple) {
        this.checkedIds = this.$refs.tree.getCheckedKeys() // 所有被选中的节点的 key 所组成的数组数据
        this.checkedData = this.$refs.tree.getCheckedNodes() // 所有被选中的节点所组成的数组数据
      } else {
        this.checkedIds = this.$refs.tree.getCurrentKey()
        this.checkedData = this.$refs.tree.getCurrentNode()
      }
      this.$emit('popoverHide', this.checkedIds, this.checkedData, this.el)
    },
    // 节点被点击时的回调,返回被点击的节点数据
    handleNodeClick(data, node) {
      if (!this.multiple) {
        const tmpMap = {}
        tmpMap.value = node.key
        tmpMap.label = node.label
        this.options = []
        this.options.push(tmpMap)
        this.selectedData = node.label
        this.isShowSelect = !this.isShowSelect
      }
    },
    // 节点选中状态发生变化时的回调
    handleCheckChange() {
      var checkedKeys = this.$refs.tree.getCheckedKeys() // 所有被选中的节点的 key 所组成的数组数据
      this.options = []
      if (!this.rootNodeChick) {
        checkedKeys.forEach(item => {
          var node = this.$refs.tree.getNode(item) // 所有被选中的节点对应的node
          const tmpMap = {}
          if (node.childNodes.length == 0) {
            tmpMap.value = node.key
            tmpMap.label = node.label
            this.options.push(tmpMap)
          }
        })
      } else {
        this.options = checkedKeys.map(item => {
          var node = this.$refs.tree.getNode(item) // 所有被选中的节点对应的node
          const tmpMap = {}
          tmpMap.value = node.key
          tmpMap.label = node.label
          return tmpMap
        })
      }
      this.selectedData = this.options.map(item => {
        return item.label
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.common-tree {
  ::v-deep .el-tree-node {
    .el-tree-node__children {
      .el-tree-node {
        .el-tree-node__content {
          padding: 0 !important;
        }
      }
    }
  }
}
</style>
